package com.tamartech.greendb.loader;

import java.util.Date;

import org.apache.log4j.Logger;

import com.tamartech.greendb.dao.ElectionDAO;
import com.tamartech.greendb.dao.ElectoralRollDAO;
import com.tamartech.greendb.dao.ElectoralRollEntryDAO;
import com.tamartech.greendb.dao.VotingInfoDAO;
import com.tamartech.greendb.dao.WardDAO;
import com.tamartech.greendb.data.Election;
import com.tamartech.greendb.data.ElectoralRoll;
import com.tamartech.greendb.data.ElectoralRollEntry;
import com.tamartech.greendb.data.VotingInfo;
import com.tamartech.greendb.data.Ward;

public class VotingInfoLoader
{
    private static Logger log = Logger.getLogger (VotingInfoLoader.class);

    private ElectoralRoll roll;
    private Election      election;
    private String        source;
    
    public VotingInfoLoader()
    {
        
    }
    
    public void begin (String wardName, 
                       Date rollDate, 
                       Date electionDate, 
                       String electionType,
                       String source) throws Exception
    {
        WardDAO wDAO = new WardDAO();
        Ward w = wDAO.get(wardName);
        
        if (w == null)
            throw new Exception ("Ward " + wardName + " not found");
        
        ElectoralRollDAO erDAO = new ElectoralRollDAO();        
        roll = erDAO.getByDate(w, rollDate);
        
        if (roll == null)
            throw new Exception ("Electoral roll for ward: " + wardName + ". date: " + rollDate + " not found");
        
        ElectionDAO eDAO = new ElectionDAO();
        election = eDAO.getByDate(electionDate, electionType);
        
        if (election == null)
            throw new Exception ("Election for date: " + electionDate + ". type: " + electionType + " not found");
        
        this.source = source;
    }
    
    public void end()
    {
        
    }
    
    public void putVoted (String pollingDistrict, int voterNumber, Integer voterNumberSuffix, String voted)
    {
            String voterNumberStr = voterNumber +
                                    (voterNumberSuffix == null ? "" : "/" + voterNumberSuffix);
            
        try
        {
            ElectoralRollEntryDAO ereDAO = new ElectoralRollEntryDAO();
            
            ElectoralRollEntry ere = ereDAO.findByReference(roll, pollingDistrict, voterNumber, voterNumberSuffix);
            
            if (ere == null)
                throw new Exception ("Electoral Roll Entry " + pollingDistrict + " " + voterNumberStr + " not found");
            
            VotingInfo vi = new VotingInfo();
            
            vi.setElection(election);
            vi.setPerson(ere.getPerson());
            vi.setVoted(voted);
            vi.setSource(source);
            
            VotingInfoDAO viDAO = new VotingInfoDAO();
            viDAO.save(vi);
            
            System.out.println ("Loaded " + pollingDistrict + " " + voterNumberStr);
        }
        catch (Exception e)
        {
            log.error("putVoted. " + pollingDistrict + " " + voterNumberStr, e);
        }
    }
}
